package com.wealth.commodity.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wealth.cart.entity.Cart;
import com.wealth.cart.service.ICartService;
import com.wealth.commodity.entity.Category;
import com.wealth.commodity.entity.Comment;
import com.wealth.commodity.entity.Goods;
import com.wealth.commodity.enums.GoodsStatusEnum;
import com.wealth.commodity.service.ICategoryService;
import com.wealth.commodity.service.ICommentService;
import com.wealth.commodity.service.IGoodsService;
import com.wealth.commodity.vo.GoodsVO;
import com.wealth.merchant.enums.MarketEnum;
import com.wealth.system.web.controller.BaseController;

import iaas.enums.StatusEnum;
import iaas.utils.ResultObject;



/**
 * 实体商品
 * @author Administrator
 */
@Controller
@RequestMapping(value="/commodity")
public class GoodsController extends BaseController{
	
	Logger logger = Logger.getLogger(GoodsController.class);
	@Autowired 
	private ICategoryService categoryService;
	@Autowired 
	private IGoodsService goodsService;
	@Autowired 
	private ICommentService commentService;
	@Autowired
	private ICartService cartService;
	
	
	
	
	/**
	 * 品牌购首页(跳转)
	 * @param request
	 * @param memberid
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/index/{marketId}",method=RequestMethod.GET)
	public String index(HttpServletRequest request, Model model,@PathVariable Long marketId) {
		try {
			//查询商品分类
			List<Category> categories=categoryService.findByCorpIdAndMarketIdAndStatus(1L, marketId,StatusEnum.NORMAL.getValue());
			model.addAttribute("categories", categories);
			//市场ID
			model.addAttribute("marketId", marketId);
			
			return "commodity/index";
		} catch (Exception e) {
			logger.error(e);
			return "system/500";
		}
	}
	
	
	
	/**
	 * 从主页面跳转到品牌购首页(跳转)
	 * @param request
	 * @param memberid
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/indexSearch",method=RequestMethod.POST)
	public String indexSearch(Model model,HttpServletRequest request,
			@RequestParam(value="searchCondition",required=false) String searchCondition) {
		try {
			//市场ID
			model.addAttribute("marketId", MarketEnum.BRAND.getValue());
			//搜索字
			model.addAttribute("searchCondition", searchCondition);
			return "commodity/index/index";
		} catch (Exception e) {
			logger.error(e);
			return "system/500";
		}
	}
	
	/**
	 * 热搜词搜索(跳转)
	 * @param request
	 * @param memberid
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/searchHotWords/{searchCondition}",method=RequestMethod.GET)
	public String searchHotWords(Model model,HttpServletRequest request,
			@PathVariable String searchCondition) {
		try {
			//市场ID
			model.addAttribute("marketId", MarketEnum.BRAND.getValue());
			//搜索字
			model.addAttribute("searchCondition", searchCondition);
			
			return "commodity/index/index";
		} catch (Exception e) {
			logger.error(e);
			return "system/500";
		}
	}
	
	
	
	
	
	/**
	 * 品牌购，本地购首页
	 * @param request
	 * @param memberid
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/indexShow")
	@ResponseBody
	public ResultObject indexshow(HttpServletRequest request, Model model,Long categoryId,Long marketId) {
		
		try {
			//商品信息
			List<GoodsVO> goods=goodsService.getPag(categoryId,GoodsStatusEnum.UP.getValue(), marketId);
			return new ResultObject("000", "查询成功", goods);
			
		} catch (Exception e) {
			logger.error(e);
			return null;
		}
	}
	
	/**
	 * 品牌购，本地购首页(搜索框)
	 * @param request
	 * @param memberid
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/search",method=RequestMethod.POST)
	@ResponseBody
	public ResultObject search(HttpServletRequest request, Model model,@RequestParam String search,@RequestParam Long  marketId,@RequestParam Long  Id) {
		
		try {
			List<GoodsVO> goodsVOs=goodsService.findByStatusAndNameLike(GoodsStatusEnum.UP.getValue(), search,marketId,Id);
			return new ResultObject("000", "查询成功", goodsVOs);
		} catch (Exception e) {
			logger.error(e);
			return new ResultObject("999", "查询错误", "");
		}
	}
	
	
	
	
	/**
	 * 品牌购，本地购首页(热门商品)
	 * @param request
	 * @param memberid
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/hotshow")
	@ResponseBody
	public ResultObject hotshow(HttpServletRequest request, Model model,Long categoryId) {
		
		try {
			//热门商品
			List<Goods> goodslist=goodsService.findTop12ByCategoryIdAndStatusOrderBySalesDesc(categoryId, GoodsStatusEnum.UP.getValue());
			return new ResultObject("000", "查询成功", goodslist);
			
		} catch (Exception e) {
			logger.error(e);
			return null;
		}
	}
	
	
	
	
	
	/**
	 * 品牌购，本地购首页(加载更多)
	 * @param request
	 * @param memberid
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/indexShowMore/{categoryId}")
	@ResponseBody
	public ResultObject indexShowMore(HttpServletRequest request, Model model,@PathVariable Long categoryId,Long maxId,Long marketId) {
		
		try {
			//商品信息
			List<GoodsVO> goods=goodsService.getPagMore(maxId, categoryId, GoodsStatusEnum.UP.getValue(),marketId);
			return new ResultObject("000", "查询成功", goods);
			
		} catch (Exception e) {
			logger.error(e);
			return null;
		}
	}
	
	/**
	 * 商品详情
	 * @param request
	 * @param goodsid
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/goodsinfo")
	public String info(HttpServletRequest request,HttpSession session, Model model,Long goodsId,Long marketId) {
		
		try {
			GoodsVO goodsVO=goodsService.getGoods(goodsId);
			model.addAttribute("goodsVO", goodsVO);
			 int count = 0;
			 if(session.getAttribute("memberid")!=null){
			  Cart cart = cartService.findCartByMemberIdAndMarketId(memberid, 3l);
					 if(cart!=null){
						 for (int i = 0; i < cart.getCartItems().size(); i++) {
							 count = count + cart.getCartItems().get(i).getAmount();
						}
					 } 
			}
			model.addAttribute("cartCount", count);
			model.addAttribute("marketId", marketId);
			return "commodity/info";
		} catch (Exception e) {
			logger.error(e);
			return null;
		}
	}
	
	
	
	/**
	 * 商品详情
	 * @param request
	 * @param goodsid
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/info")
	public String info(HttpServletRequest request, Model model,Long goodsId) {
		
		try {
			GoodsVO goodsVO=goodsService.getGoods(goodsId);
			model.addAttribute("goodsVO", goodsVO);
			 //购物车 
			 Cart cart=cartService.findCartByMemberIdAndMarketId(memberid, 3L);
			 int count = 0;
			 if(cart!=null){
				 for (int i = 0; i < cart.getCartItems().size(); i++) {
					 count = count + cart.getCartItems().get(i).getAmount();
				}
			 }
			model.addAttribute("cartCount", count);
			model.addAttribute("marketId", goodsVO.getGoods().getMerchant().getMarket().getId());
			return "commodity/info";
		} catch (Exception e) {
			logger.error(e);
			return null;
		}
	}
	
	/**
	 * 商品评价页面
	 * @param request
	 * @param goodsid
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/comment")
	public String comment(HttpServletRequest request, Model model,Long goodsId,Long marketId) {
		
		try {
			
			GoodsVO goodsVO=goodsService.getGoods(goodsId);
			model.addAttribute("goodsVO", goodsVO);

			model.addAttribute("marketId", marketId);
			
			return "commodity/comment";
		} catch (Exception e) {
			logger.error(e);
			return null;
		}
	}
	
	/**
	 * 加载商户下 商品评论
	 */
	@RequestMapping(value="/more",method=RequestMethod.POST)
	@ResponseBody
	public ResultObject list(
			@RequestParam(defaultValue="-1") Long merchantid,
			@RequestParam(defaultValue="-1") Long goodsid,
			@RequestParam(defaultValue="-1") Integer index,
			@RequestParam(defaultValue="10") Integer offset) {
		try{
			if(goodsid==-1){
				List<Comment> orderList = commentService.ajaxCommentByMerchantId(merchantid, index, offset);
				return new ResultObject("000", "加载成功", orderList);
			}else{
				List<Comment> orderList = commentService.ajaxComment(goodsid, index, offset);
				return new ResultObject("000", "加载成功", orderList);
			}
		}catch(Exception e){
			return new ResultObject("999", "加载错误", "");
		}
	}
	
		
	
	
}
